//*********************************************************
//构建树
//*********************************************************
var dataSource;
var tableId;
var rootId;

//构造一个层，用来显示正在加载数据
document.write("<div id=\"loading\" style=\"z-index: 9999;width: 100%;height: 100%;position:absolute;left:10px;top: 0px;display:none;\">");
document.write("<table width=\"100%\" height=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
document.write("<tr>");
document.write("<td align=\"center\" valign=\"middle\">");
document.write("<img src=\"../images/index/loading.gif\">");
document.write("</td>");
document.write("</tr>");
document.write("</table>");
document.write("</div>");

function createTree(table,root,url){
	cleanTable(document.getElementById(table));
	dataSource = url;
	rootId = root;
	tableId = table;
	makeTree(rootId,"appand");
}

function userTree(table,root,url){
	cleanTable(document.getElementById(table));
	dataSource = url;
	rootId = root;
	tableId = table;
	makeTree(rootId,"appand");
}

function cleanTable(tObj){
	var length = tObj.rows.length;
	for(var i = 0; i < length; i++){
		tObj.deleteRow(0);
	}
}

function makeTree(rowID, choose) {
	var ids = rowID.split("_");
	var tObj = document.getElementById(tableId);
	if ("appand" == choose) {
		showLoading();
		$.getJSON(dataSource + ids[ids.length - 1] + "&requestTime=" + new Date().getTime(), function (json) {
			var treeNodes = json.treeNodes;
			for (var i = 0; i < treeNodes.length; i++) {
				if (rowID == rootId) {
					createRow(tObj,tObj.rows.length, treeNodes[i]);
				} else {
					var selectRow = document.getElementById(rowID);
					createRow(tObj,selectRow.rowIndex + 1, treeNodes[i]);
				}
			}
			hiddenLoading();
		});
	} else {
		depand(tObj,rowID);
	}
}
//*********************************************************
//添加行
//*********************************************************
function createRow(tObj,rowIndex,treeNode) {
	//根据索引添加新行
	var newRow = tObj.insertRow(rowIndex);
	//设置行ID
	newRow.id = treeNode.rowId;
	//根据行对象添加单元格
	for (var i = 0; i < treeNode.cells.length; i++) {
		var newCell = newRow.insertCell(i);
		//设置单元格内容
		newCell.innerHTML = treeNode.cells[i].innerHTML;
		//根据情况设置单元格宽度
		if (treeNode.cells[i].cellWidth != -1) {
			newCell.width = treeNode.cells[i].cellWidth;
		}
		//根据情况设置单元格高度
		if (treeNode.cells[i].cellHeight != -1) {
			newCell.height = treeNode.cells[i].cellHeight;
		}
		//根据情况设置单元格样式
		if(treeNode.cells[i].className != ""){
			newCell.className = treeNode.cells[i].cssClass;
		}
		//根据情况设置单元格跨行数
		if(treeNode.cells[i].rowspan != -1){
			newCell.rowSpan = treeNode.cells[i].rowspan;
		}
		//根据情况设置单元格跨列数
		if(treeNode.cells[i].colspan != -1){
			newCell.colSpan = treeNode.cells[i].colspan;
		}
		//根据情况设置单元格背景颜色
		if(treeNode.cells[i].bgColor != null){
			newCell.bgColor = treeNode.cells[i].bgColor;
		}
		//根据情况设置单元格水平对齐和垂直对齐
		newCell.align = treeNode.cells[i].align;
		newCell.valign = treeNode.cells[i].valign;
	}
}
//************************************************************
//展开节点
//************************************************************
function appand(imgID) {
	//根据ID获得图片对象
	var img = document.getElementById(imgID);
	//获得图片的格式
	var imgType = img.src.substring(img.src.lastIndexOf("."));
	//获得图片的路径
	var imgUrl = img.src.substring(0, img.src.lastIndexOf("/") + 1);
	//获得图片名称
	var imgName = img.src.substring(img.src.lastIndexOf("/") + 1, img.src.lastIndexOf("."));
	//根据图片名称替换图片close对应open
	if (imgName.indexOf("close") != -1) {
		if(imgName.length > 5){
			img.src = imgUrl + "open" + imgName.substring(5,imgName.length) + imgType;
		}else{
			img.src = imgUrl + "open" + imgType;
		}
		makeTree(imgID.substring(4, imgID.length), "appand");
	} else if(imgName.indexOf("open") != -1){
		if(imgName.length > 4){
			img.src = imgUrl + "close" + imgName.substring(4,imgName.length) + imgType;
		}else{
			img.src = imgUrl + "close" + imgType;
		}
		makeTree(imgID.substring(4, imgID.length), "depand");
	}
}
//*******************************************************
//收拢节点
//*******************************************************
function depand(tObj,nodeId){
	var nodeIds = nodeId.split("_");
	var rows = tObj.rows;
	var rowsLength = rows.length;
	//记录需要删除的节点的ID
	var deleteIds = new Array();
	var superRow = new Array();
	for(var i = 0;i < rowsLength;i++){
		if(rows[i].id.indexOf(nodeIds[nodeIds.length - 1] + "_") != -1){
			if(rows[i].id == nodeIds[nodeIds.length - 1] + "_" + nodeIds[nodeIds.length - 1]){
				superRow[superRow.length] = rows[i].id;
			}else{
				deleteIds[deleteIds.length] = rows[i].id;
			}
		}
	}
	
	//删除特殊行,上级ID和本级ID相同的行
	for(var k = 0 ;k < superRow.length;k++){
		var selectRow = document.getElementById(superRow[k]);
		tObj.deleteRow(selectRow.rowIndex);		
	}
	
	//根据记录的节点ID集合删除节点
	for(var j = 0 ;j < deleteIds.length;j++){
		var selectRow = document.getElementById(deleteIds[j]);
		depand(tObj,deleteIds[j]);
		tObj.deleteRow(selectRow.rowIndex);		
	}
}
//********************************************************
//重新加载节点
//********************************************************
function reloadNode(imgID){

	var img = document.getElementById(imgID);
	//获得图片的格式
	var imgType = img.src.substring(img.src.lastIndexOf("."));
	//获得图片的路径
	var imgUrl = img.src.substring(0, img.src.lastIndexOf("/") + 1);
	//获得图片名称
	var imgName = img.src.substring(img.src.lastIndexOf("/") + 1, img.src.lastIndexOf("."));
	//根据图片名称替换图片close对应open
	if (imgName.indexOf("close") != -1) {
		makeTree(imgID.substring(4, imgID.length), "appand");
		img.src = imgUrl + "open" + imgName.substring(5,imgName.length) + imgType;
	} else if(imgName.indexOf("open") != -1){
		makeTree(imgID.substring(4, imgID.length), "depand");
		makeTree(imgID.substring(4, imgID.length), "appand");
		img.src = imgUrl + "open" + imgName.substring(4,imgName.length) + imgType;
	}
}
function removeRow(tObj,nodeID) {
	
	if(nodeID == rootId){
		var length = tObj.rows.length;
		for(var i = 0;i < length;i++){
			tObj.deleteRow(0);
		}
	}else{
		var selectRow = document.getElementById(nodeID);
		tObj.deleteRow(selectRow.rowIndex + 1);
	}
}
function deleteNode(nodeId){
	var tObj = document.getElementById(tableId);
	var selectRow = document.getElementById(nodeId);
	tObj.deleteRow(selectRow.rowIndex);
}

//选择多个填入表单
function clickDepart(idFild,nameFild,id,name){
	var inId = document.getElementById(idFild);
	var inName = document.getElementById(nameFild);
	if(inId.value.indexOf(id) == -1){
		inId.value = inId.value + id + ",";
		inName.value = inName.value + name + ",";
	}else{
		inId.value = inId.value.substring(0,inId.value.indexOf(id)) + inId.value.substring(inId.value.indexOf(id) + id.length + 1,inId.length);
		inName.value = inName.value.substring(0,inName.value.indexOf(name)) + inName.value.substring(inName.value.indexOf(name) + name.length + 1,inName.length);
	}
}

//选择单个填入表单
function clickUser(idFild,nameFild,id,name){
	var inId = document.getElementById(idFild);
	var inName = document.getElementById(nameFild);
	if(inId.value.indexOf(id) == -1){
		inId.value =  id ;
		inName.value = name ;
	}else{
		inId.value = inId.value.substring(inId.value.indexOf(id) + id.length + 1,inId.length);
		inName.value = inName.value.substring(inName.value.indexOf(name) + name.length + 1,inName.length);
	}
	inName.focus();
}

//显示层
function showDiv(divId){
	var selectDiv = document.getElementById(divId);
	selectDiv.style.display = 'block';
}
//隐藏层
function hiddenDiv(divId){
	var selectDiv = document.getElementById(divId);
	selectDiv.style.display = 'none';
}
//显示正在读取数据层
function showLoading(){
	showDiv('loading');
}
//隐藏正在读取数据层
function hiddenLoading(){
	hiddenDiv('loading');
}
